import os

from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# 数据库配置
DATABASE_HOST = os.getenv('DB_HOST', 'localhost')
DATABASE_PORT = os.getenv('DB_PORT', '5432')
DATABASE_NAME = os.getenv('DB_NAME', 'hydropulse')
DATABASE_USER = os.getenv('DB_USER', 'postgres')
DATABASE_PASSWORD = os.getenv('DB_PASSWORD', 'password')

# 应用配置
APP_NAME = os.getenv('APP_NAME', 'HydroPulse-DX')
APP_VERSION = os.getenv('APP_VERSION', '1.0.0')
DEBUG = os.getenv('DEBUG', 'False').lower() == 'true'

# TimescaleDB配置
TIMESCALEDB_CHUNK_TIME_INTERVAL = os.getenv('TIMESCALEDB_CHUNK_TIME_INTERVAL', '1 day')
TIMESCALEDB_COMPRESSION_AFTER = os.getenv('TIMESCALEDB_COMPRESSION_AFTER', '7 days')

# 数据生成配置 - 大规模数据测试
DEFAULT_STATION_COUNT = int(os.getenv('DEFAULT_STATION_COUNT', '100'))
DEFAULT_RECORDS_PER_STATION = int(os.getenv('DEFAULT_RECORDS_PER_STATION', '1000000'))
BATCH_SIZE = int(os.getenv('BATCH_SIZE', '50000'))  # 增大批次以提升插入性能


def get_database_url() -> str:
    """获取数据库连接URL
    
    Returns:
        数据库连接URL
    """
    return f"postgresql://{DATABASE_USER}:{DATABASE_PASSWORD}@{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_NAME}"


def get_database_config() -> dict:
    """获取数据库配置
    
    Returns:
        数据库配置字典
    """
    return {
        'host': DATABASE_HOST,
        'port': DATABASE_PORT,
        'database': DATABASE_NAME,
        'user': DATABASE_USER,
        'password': DATABASE_PASSWORD,
        'url': get_database_url(),
        'timescale_enabled': True
    }


def get_app_config() -> dict:
    """获取应用配置
    
    Returns:
        应用配置字典
    """
    return {
        'name': APP_NAME,
        'version': APP_VERSION,
        'debug': DEBUG,
        'database_url': get_database_url()
    }


def get_timescaledb_config() -> dict:
    """获取TimescaleDB配置
    
    Returns:
        TimescaleDB配置字典
    """
    return {
        'chunk_time_interval': TIMESCALEDB_CHUNK_TIME_INTERVAL,
        'compression_after': TIMESCALEDB_COMPRESSION_AFTER
    }


def get_data_generation_config() -> dict:
    """获取数据生成配置
    
    Returns:
        数据生成配置字典
    """
    return {
        'default_station_count': DEFAULT_STATION_COUNT,
        'default_records_per_station': DEFAULT_RECORDS_PER_STATION,
        'batch_size': BATCH_SIZE
    }
